package controllers

import (
	"goconfcenter/core/consts"
	"goconfcenter/core/response"
	"goconfcenter/models"
	"goconfcenter/pkg/Util"
	"goconfcenter/pkg/snowflake"
	"goconfcenter/service"
	"goconfcenter/service/dto/bus"
	"strconv"
	"time"

	"github.com/ahmetb/go-linq/v3"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type RoleController struct {
}

func (c *RoleController) DataGrid(ctx *gin.Context) {
	params := new(bus.RoleQuery)
	if err := ctx.ShouldBindJSON(params); err != nil {
		zap.L().Error("请求参数异常", zap.Error(err))
		response.ErrorParam(ctx, "")
		return
	}
	data, total := service.RoleServiceInstance().PageByQuery(params)
	var pageData response.PageData
	pageData.Data = data
	pageData.Total = total
	response.Success(ctx, consts.CurdStatusOkMsg, pageData)
}

func (c *RoleController) Add(ctx *gin.Context) {
	userId := ctx.GetInt64("userId")
	role := new(models.Role)
	if err := ctx.ShouldBindJSON(role); err != nil {
		zap.L().Error("请求参数异常", zap.Error(err))
		response.ErrorParam(ctx, "")
		return
	}
	loginUser := service.BackendUserServiceInstance().GetInfo(userId)
	role.ID = snowflake.GenID()
	role.Creator = loginUser.ID
	role.Updater = loginUser.ID
	role.CreateTime = time.Now()
	role.UpdateTime = time.Now()
	result := service.RoleServiceInstance().Insert(role)
	if result == nil {
		response.Fail(ctx, consts.BusinessFail, consts.BusinessFailMsg, "添加角色失败")
	} else {
		response.Success(ctx, consts.CurdStatusOkMsg, result.ID)
	}
	return
}
func (c *RoleController) Get(ctx *gin.Context) {
	id, _ := strconv.ParseInt(ctx.DefaultQuery("id", "0"), 10, 64)
	result := service.RoleServiceInstance().Get(id)
	response.Success(ctx, consts.CurdStatusOkMsg, result)
	return
}

func (c *RoleController) Edit(ctx *gin.Context) {
	userId := ctx.GetInt64("userId")
	role := new(models.Role)
	if err := ctx.ShouldBindJSON(role); err != nil {
		zap.L().Error("请求参数异常", zap.Error(err))
		response.ErrorParam(ctx, "")
		return
	}
	oldRole := service.RoleServiceInstance().Get(role.ID)
	loginUser := service.BackendUserServiceInstance().GetInfo(userId)
	oldRole.Name = role.Name
	oldRole.Mark = role.Mark
	oldRole.Status = role.Status
	oldRole.Updater = loginUser.ID
	oldRole.UpdateTime = time.Now()
	result := service.RoleServiceInstance().Update(oldRole)
	if result {
		response.Success(ctx, consts.CurdStatusOkMsg, oldRole.ID)
	} else {
		response.Fail(ctx, consts.BusinessFail, consts.BusinessFailMsg, "编辑失败")
	}
	return
}

func (c *RoleController) ResTreeGrid(ctx *gin.Context) {
	id, _ := strconv.ParseInt(ctx.DefaultQuery("id", "0"), 10, 64)
	role := service.RoleServiceInstance().Get(id)
	if role == nil {
		response.Fail(ctx, consts.BusinessFail, consts.BusinessFailMsg, "数据不存在")
	}
	resourceList := service.ResourceServiceInstance().GetList()
	resourceRelList := service.RoleResourceRelServiceInstance().GetListByRoleId(id)
	for i, v := range resourceList {
		one := linq.From(resourceRelList).WhereT(func(a *models.RoleResourceRel) bool {
			return v.ID == a.ResourceId
		}).First()
		if one != nil {
			resourceList[i].IsChecked = true
		}
	}
	response.Success(ctx, consts.CurdStatusOkMsg, resourceList)
	return
}
func (c *RoleController) Auth(ctx *gin.Context) {
	userId := ctx.GetInt64("userId")
	auth := new(bus.RoleAuthResourceReq)
	if err := ctx.ShouldBindJSON(auth); err != nil {
		zap.L().Error("请求参数异常", zap.Error(err))
		response.ErrorParam(ctx, "")
		return
	}
	resIds := Util.StringToInt64(auth.ResIds)
	result := service.RoleResourceRelServiceInstance().SaveRel(auth.ID, resIds, userId)
	response.Success(ctx, consts.CurdStatusOkMsg, result)
	return
}
